﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.ComponentModel.Design;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using T.ApiResults;
using T.Basic.API.Appliction.Command.FuelCostCommand;
using T.Basic.API.Appliction.Command.LineCommand;
using T.Basic.API.Appliction.Command.OutsourcedUnitsCommand;
using T.Basic.API.Appliction.Command.VehicleCommand;
using T.Basic.Domain.T.Basic.Model;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;

namespace T.Basic.API.Controllers
{
    /// <summary>
    /// 车辆信息控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [Authorize]//JWT验证
    [ApiController]
    public class VehicleController : ControllerBase
    {
        /// <summary>
        /// 中介者
        /// </summary>
        private readonly IMediator _mediator;
        public VehicleController(IMediator mediator)
        {
            _mediator = mediator;
        }
        /// <summary>
        /// 添加车辆信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> CreateVehicle(VehicleCreateCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                command.CompanyID = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//公司ID
                command.User_Id = int.Parse(HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value);//用户ID
                command.CreatedBy = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//创建人
                result.Code = await _mediator.Send(command);
                //result.Code = await _mediator.Send(new VehicleCreateCommand()
                //{

                //    CompanyID = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value,
                //    User_Id = int.Parse(HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value),
                //    BrandAndModel = command.BrandAndModel,
                //    PlateNumber = command.PlateNumber,
                //    DriverName = command.DriverName,
                //    BelongingCompany = command.BelongingCompany,
                //    VehicleLong = command.VehicleLong,
                //    VehicleWide = command.VehicleWide,
                //    VehicleTall = command.VehicleTall,
                //    VehicleColor = command.VehicleColor,
                //    AcquisitionDate = command.AcquisitionDate,
                //    TradingCard = command.TradingCard,
                //    InsuranceExpirationDate = command.InsuranceExpirationDate,
                //    AnnualInspectionExpiratDate = command.AnnualInspectionExpiratDate,
                //    MaintenanceMileageSetting = command.MaintenanceMileageSetting,
                //    VehicleImg = command.VehicleImg,
                //    InsuranceCarImg = command.InsuranceCarImg,
                //    CreatedBy = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value,
                //    CreatedDate = DateTime.Now,


                //});
            }
            catch (Exception)
            {
                result.Code = -1;
                result.Message = "添加车辆信息失败";
                throw;
            }
            return result;
        }
        /// <summary>
        /// 删除车辆信息（批量）
        /// </summary>
        /// <param name="Command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> DeleteAllVehicle(VehicleDeleteAllCommand Command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                Command.DeletedBy = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//删除人
                Command.DeletedDate = DateTime.Now;//删除时间
                result.Code = await _mediator.Send(Command);
                //result.Code = await _mediator.Send(new VehicleDeleteAllCommand()
                //{
                //    VehicleIds = Command.VehicleIds,
                //    DeletedBy = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value,
                //    DeletedDate = DateTime.Now,
                //});
            }
            catch (Exception)
            {
                result.Code = -1;
                result.Message = "批删车辆信息失败";
                throw;
            }
            return result;
        }
        /// <summary>
        /// 删除车辆信息（单删）
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> DeleteVehicle([FromQuery]VehicleDeleteCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                command.DeletedBy = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//删除人
                command.DeletedDate = DateTime.Now;//删除时间
                result.Code = await _mediator.Send(command);
                //result.Code = await _mediator.Send(new VehicleDeleteCommand()
                //{
                //    VehicleId = command.VehicleId,
                //    DeletedBy = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value,
                //    DeletedDate = DateTime.Now,
                //});
            }
            catch (Exception)
            {
                result.Code = -1;
                result.Message = "删除（单删）车辆信息失败";
                throw;
            }
            return result;
        }
        /// <summary>
        /// 反填车辆信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<Vehicle>> GetVehicleByAsync([FromQuery] VehicleGetByIdCommand command)
        {
            ApiResult<Vehicle> result = new ApiResult<Vehicle>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
            //try
            //{
            //    return await _mediator.Send(command);
            //}
            //catch (Exception)
            //{

            //    throw;
            //}
        }
        /// <summary>
        /// 修改车辆信息
        /// </summary>
        /// <param name="VehicleUpdateCommand"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdateVehicle(VehicleUpdateCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                command.CompanyID = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//公司ID
                command.User_Id = int.Parse(HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value);//用户ID
                command.UpdatedBy = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//修改人
                command.UpdatedDate = DateTime.Now;//修改时间
                result.Code = await _mediator.Send(command);
                //result.Code = await _mediator.Send(new VehicleUpdateCommand()
                //{
                //    VehicleId = command.VehicleId,
                //    CompanyID = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value,
                //    User_Id = int.Parse(HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value),
                //    BrandAndModel = command.BrandAndModel,
                //    PlateNumber = command.PlateNumber,
                //    DriverName = command.DriverName,
                //    BelongingCompany = command.BelongingCompany,
                //    VehicleLong = command.VehicleLong,
                //    VehicleWide = command.VehicleWide,
                //    VehicleTall = command.VehicleTall,
                //    VehicleColor = command.VehicleColor,
                //    AcquisitionDate = command.AcquisitionDate,
                //    TradingCard = command.TradingCard,
                //    InsuranceExpirationDate = command.InsuranceExpirationDate,
                //    AnnualInspectionExpiratDate = command.AnnualInspectionExpiratDate,
                //    MaintenanceMileageSetting = command.MaintenanceMileageSetting,
                //    VehicleImg = command.VehicleImg,
                //    InsuranceCarImg = command.InsuranceCarImg,
                //    CreatedBy = command.CreatedBy,
                //    CreatedDate = command.CreatedDate,
                //    UpdatedBy = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value,
                //    UpdatedDate = DateTime.Now,
                //    DeletedBy = command.DeletedBy,
                //    DeletedDate = command.DeletedDate,


                //});
            }
            catch (Exception)
            {
                result.Code = -1;
                result.Message = "修改车辆信息失败";
                throw;
            }
            return result;

        }
    }
}
